package com.example.bestpractices;

import android.R.bool;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;





public class MainActivity extends ActionBarActivity {

	Button check_button;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        check_button = (Button) findViewById(R.id.button1);
        checkStatus();
        
    }
    private void checkStatus() {
		
		check_button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			int status;
			int chargeSource;
			boolean batteryCharging=false; 
			boolean batteryNotCharging= false;
			IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
			Intent batteryStatus = registerReceiver(null, intentFilter);
			status= batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
			if(status==BatteryManager.BATTERY_STATUS_CHARGING)
				batteryCharging= true;
			if(status==BatteryManager.BATTERY_STATUS_NOT_CHARGING)
				batteryNotCharging = true;
			
			if(batteryCharging)
			{
				chargeSource = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
				if(chargeSource==BatteryManager.BATTERY_PLUGGED_AC)
					Toast.makeText(MainActivity.this, "AC Plugged in", Toast.LENGTH_LONG).show();
				if(chargeSource==BatteryManager.BATTERY_PLUGGED_USB)
					Toast.makeText(MainActivity.this, "USB Charging", Toast.LENGTH_LONG).show();
			}
		}
		});
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
